What are tag libraries ?

Tag libraries are collections of special tags that add extra functionality to your view layer (mostly JSP's). These special tags are proceeded by a namespace (ww:, webwork:, jsp:, ..) and are provided to reduce the code you have to write in your view layer.

WebWork Tag library

WebWork tags are no different than any other tag library. You declare them at the top of your page:

<%@ taglib prefix="ww" uri="/webwork" %>

And then you can use them to display (bean) properties, iterate collections, create forms, show l10n text, create dropdown boxes, and a whole lot more.

WebWork tags come in different flavors. Some are what we call form tags; they can be used to create complex forms very quickly and in only a few lines that would normally require a great deal of complex code. Other tags are used to control the flow (if/else), control data (bean, push, set) or just display it (property). WebWork contains a lot of tags, and good knowledge of them will give your productivity a huge boost.

About themes

Form tags use so-called themes: these themes add extra markup to your tags. One theme might just add tables and labels, while another theme might allow ajax functionality. The default theme is xhtml, unless you override it in the theme attribute, the parent tag's attribute or the webwork.properties file. The xhtml theme will add labels, validation errors and uses a table-based layout.

For example:

<ww:textfield name="test" value="%{test}" label="getText('name'}"/>

In simple mode:

<input type="text" name="test" value="test" id="foo_test"/>

In xhtml mode:

<tr>
    <td class="tdLabel">
        <label for="foo_test" class="label">Name:</label>
    </td>
    <td>
        <input type="text" name="test" value="test" id="foo_test"/>
    </td>
</tr>

This clearly shows that a good theme will greatly reduce the amount of code you have to write. Therefore, if you find yourself in need of a special layout (say, a three column layout), you'll often find it a lot easier in the long run to create a custom theme (or override one), than using scriptlets or using the simple theme.

Gotcha's

There are some things you'll need to keep in mind while using the ww tags. Some might not entirely work as you expect at first.

Take the ww:textfield tag again (see above). You'll notice that the value attribute has a value of %{test}. The %{..} tells WW to look up the property on the valuestack (resulting in a call to the getTest() method on the Action) rather than taking the String literal 'test' (notice the difference with the name attribute, which does in fact use the String 'test'. This is all pretty normal.

However, there are some places where the %{..} notation is not required; take the ww:iterator tag. It takes a value attribute, so you could use it to iterate a collection you get from the valuestack. For example, getNames() returns a List with names. Now, since there is no possibility to iterate a String literal, you can drop the %{..} notation, and just use value="names" instead of value="%{names}" (although the latter will work as well). Still no biggie.

However, there are some tags where things will work differently from what you expect. The ww:set tag, for example, allows you to store a certain object in a different scope. Let's say we want to store a message in our session. This is what you might expect to work:

<ww:set name="message" value="all your base are belong to us" scope="session"/>

Unfortuneatly, this will not work. Nothing will get stored in the session. Why ?

The reason for this is that the value attribute expects an expression rather than a String literal. Therefor, if you want to store this message, you'll have to ask the valuestack to create a String for you:

<ww:set name="message" value="%{'all your base are belong to us'}" scope="session"/>

Therefore, it's very important to read the attribute information for each tag (look them up in the documentation!). For example, for the ww:set tag, this is:

  • name: String
  • value: Object
  • .. (others skipped for brevity)
    Keep this in mind whenever a tag does not function like you would expect, esp. when dealing with booleans !

Tutorial

Coming soon.